﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.DAL;

namespace TravelLink.WebApp.Admin
{
    public partial class AdminCompanyService : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckResponse("2002");
            if (!this.IsPostBack)
            {
                if (this.Request.QueryString["id"] != string.Empty)
                {
                    this.LinqDataSource1.Where = @"CompanyCode.ToString()=@CompanyCode";
                    this.LinqDataSource1.WhereParameters.Add("CompanyCode", this.Request.QueryString["id"]);
                }
            }
        }

        protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            TL_Admin opr = Session["Admin"] as TL_Admin;
            if (this.Request.QueryString["id"]!=null&&this.Request.QueryString["id"] != string.Empty)
            {
                ((TL_Company_Service)e.NewObject).CompanyCode = Guid.Parse(this.Request.QueryString["id"]);
            }
            ((TL_Company_Service)e.NewObject).CommissionType = 1;
            ((TL_Company_Service)e.NewObject).CreateOpr = opr.Idx;
            ((TL_Company_Service)e.NewObject).Paid = false;
        }

        protected void ASPxGridView1_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
        {
            int i = 0;
        }

        protected void ASPxGridView1_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
        {
            if (e.VisibleIndex < 0)
            {
                return;
            }
            TL_Company_Service obj = this.ASPxGridView1.GetRow(e.VisibleIndex) as TL_Company_Service;
            if (e.DataColumn.Caption == "总价")
            {              
                e.Cell.Text = (obj.Price * obj.Qty).ToString("f2");
            }
            if (e.DataColumn.Caption == "提奖计算")
            {
                decimal amount = obj.Commission;
                if (obj.CommissionType == 2)
                {
                    amount= (obj.Price * obj.Qty)*obj.Commission;
                }
                e.Cell.Text = amount.ToString("f2");
            }
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            //this.LinqDataSource1.Where = @"Idx.ToString().IndexOf(@key)>-1||Name.IndexOf(@key)>-1||Content.IndexOf(@key)>-1||CompanyCode.ToString().IndexOf(@key)>-1||TL_Company.CompanyName.IndexOf(@key)>-1||TL_Company.CompanyNameEN.IndexOf(@key)>-1||TL_Company.Email.IndexOf(@key)>-1||TL_Company.ContactMobile.IndexOf(@key)>-1||TL_Company.Brand.IndexOf(@key)>-1";
            this.LinqDataSource1.Where = @"Idx.ToString()=@key||Name=@key||Content=@key||CompanyCode.ToString()=@key||TL_Company.CompanyName=@key||TL_Company.CompanyNameEN=@key||TL_Company.Email=@key||TL_Company.ContactMobile=@key||TL_Company.Brand=@key";
            this.LinqDataSource1.WhereParameters.Add("key", this.ASPxTextBox1.Text);
        }

        protected void abtnExport_Click(object sender, EventArgs e)
        {
            this.ASPxGridViewExporter1.WriteXlsToResponse(true);
        }
    }
}